package com.youkeda.app.dataobject;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

import com.youkeda.app.model.PreSingleRedEnvelope;
import com.youkeda.app.model.PreSingleRedEnvelopeStatus;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;

/**
 * 子红包
 * @author 
 */
@Data
public class PreSingleRedEnvelopeDO implements Serializable {
    private String id;

    private String redEnvelopeId;

    private BigDecimal redEnvelopeAmount;

    /**
     * 状态
     */
    private String status;

    private String note;

    private Date gmtCreated;

    private Date gmtModified;

    private static final long serialVersionUID = 1L;

    public PreSingleRedEnvelopeDO(PreSingleRedEnvelope preSingleRedEnvelope) {
        if (preSingleRedEnvelope.getGmtCreated() != null) {
            this.setGmtCreated(
                    Date.from(preSingleRedEnvelope.getGmtCreated().atZone(ZoneId.systemDefault()).toInstant()));
        }
        if (preSingleRedEnvelope.getGmtModified() != null) {
            this.setGmtModified(
                    Date.from(preSingleRedEnvelope.getGmtModified().atZone(ZoneId.systemDefault()).toInstant()));
        }
        if (preSingleRedEnvelope.getStatus() != null) {
            this.setStatus(preSingleRedEnvelope.getStatus().toString());
        }
        if (preSingleRedEnvelope.getRedEnvelopeAmount() != 0) {
            this.setRedEnvelopeAmount(new BigDecimal(preSingleRedEnvelope.getRedEnvelopeAmount()));
        }
        this.setRedEnvelopeId(preSingleRedEnvelope.getRedEnvelopeId());
        this.setNote(preSingleRedEnvelope.getNote());
        if (!StringUtils.isEmpty(preSingleRedEnvelope.getId())) {
            this.setId(preSingleRedEnvelope.getId());
        }
    }

    public PreSingleRedEnvelope convertToModel() {
        PreSingleRedEnvelope preSingleRedEnvelope = new PreSingleRedEnvelope();
        BeanUtils.copyProperties(this, preSingleRedEnvelope);
        if (this.getRedEnvelopeAmount() != null) {
            preSingleRedEnvelope.setRedEnvelopeAmount(this.getRedEnvelopeAmount().doubleValue());
        }
        if (!StringUtils.isEmpty(this.getStatus())) {
            preSingleRedEnvelope.setStatus(PreSingleRedEnvelopeStatus.valueOf(this.getStatus()));
        }
        Date date = this.getGmtCreated();
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        preSingleRedEnvelope.setGmtCreated(localDateTime);
        Date date1 = this.getGmtModified();
        Instant instant1 = date1.toInstant();
        ZoneId zoneId1 = ZoneId.systemDefault();
        LocalDateTime localDateTime2 = instant1.atZone(zoneId1).toLocalDateTime();
        preSingleRedEnvelope.setGmtModified(localDateTime2);
        return preSingleRedEnvelope;
    }
}